package com.uplooking.controller;

import java.io.OutputStream;

import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.uplooking.service.CustService;

@Controller
public class CustController {

	@Autowired
	private CustService custService;

	@RequestMapping(value = "/cust/index", method = RequestMethod.GET)
	public String methodMenu7() {
		return "resource/cust";
	}

	@ResponseBody
	@RequiresPermissions("cust!list")
	@RequestMapping(value = "/cust/list", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })
	public String methodCust1(String ctid, int index, int size, int step) throws Exception {
		String text = JSONObject.toJSONString(custService.getCustList(ctid, index, size, step));
		System.out.println(text);
		return text;
	}
	@RequiresPermissions("cust!list")
	@RequestMapping(value = "/cust/showctid", method = RequestMethod.GET)
	public void methodCust11(String id,HttpServletResponse resp) throws Exception {
		OutputStream os =resp.getOutputStream();
		byte[] bs =custService.getCustById(id).getCtidinfo();
		os.write(bs);
		os.flush();
		os.close();
	}
	
	@RequiresPermissions("cust!list")
	@RequestMapping(value = "/cust/showctdid", method = RequestMethod.GET)
	public void methodCust12(String id,HttpServletResponse resp) throws Exception {
		OutputStream os =resp.getOutputStream();
		byte[] bs =custService.getCustById(id).getCtdidinfo();
		os.write(bs);
		os.flush();
		os.close();
	}

	@ResponseBody
	@RequiresPermissions("cust!add")
	@RequestMapping(value = "/cust/add", method = RequestMethod.POST, produces = { "application/json;charset=utf-8" })
	public String methodCust2(String ctid, MultipartFile ctidinfo, MultipartFile ctdidinfo, String ctname, String ctsex,
			String ctaddr, String ctphone, String ctcareer, String ctdesc) throws Exception {
		String text = JSONObject.toJSONString(custService.addCust("SFZ" + ctid, ctidinfo, ctdidinfo, ctname, ctsex,
				ctaddr, ctphone, ctcareer, ctdesc));
		System.out.println(text);
		return text;
	}

	@ResponseBody
	@RequiresPermissions("cust!update")
	@RequestMapping(value = "/cust/update", method = RequestMethod.POST, produces = {
			"application/json;charset=utf-8" })
	public String methodCust3(String ctid, MultipartFile ctidinfo, MultipartFile ctdidinfo, String ctname, String ctsex,
			String ctaddr, String ctphone, String ctcareer, String ctdesc) throws Exception {
		String text = JSONObject.toJSONString(
				custService.updateCust(ctid, ctidinfo, ctdidinfo, ctname, ctsex, ctaddr, ctphone, ctcareer, ctdesc));
		System.out.println(text);
		return text;
	}

	@ResponseBody
	@RequiresPermissions("cust!lock")
	@RequestMapping(value = "/cust/lock", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })
	public String methodCust4(String ctid) throws Exception {
		String text = JSONObject.toJSONString(custService.lockCust(ctid));
		System.out.println(text);
		return text;
	}

	@ResponseBody
	@RequiresPermissions("cust!lock")
	@RequestMapping(value = "/cust/unlock", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })
	public String methodCust5(String ctid) throws Exception {
		String text = JSONObject.toJSONString(custService.unLockCust(ctid));
		System.out.println(text);
		return text;
	}

	@ResponseBody
	@RequiresPermissions("cust!out")
	@RequestMapping(value = "/cust/out", method = RequestMethod.GET, produces = { "application/json;charset=utf-8" })
	public String methodCust6(String ctid) throws Exception {
		String text = JSONObject.toJSONString(custService.outCust(ctid));
		System.out.println(text);
		return text;
	}
}
